# -*- coding: utf-8 -*-
import mysqlDB
class O2oDesc:
    def __init__(self):
        # self.odb =mysqlDB.Mydb(host='localhost', dataBase='smw01_erp0911', user='smw01_erp0911', passwd='ZMwj3BSwtVTMS32r')
        self.odb =mysqlDB.Mydb(host='localhost', dataBase='j_com', user='root', passwd='123456')

    def update(self):
        self.odb.dbLoad()
        query = self.odb.query(u"SELECT `id`,`user_id`,`product_id`,`status` FROM `product_o2o` order by id DESC;")
        sale=0
        for row in query:
            sql="SELECT p.code,p.image,p.ygstatus,p.name,p.mian_cat cate_id,p.price jiayi,ppo.price,ppo.add,ppo.percent FROM `product_price_o2o` as ppo LEFT JOIN product as p on ppo.product_id = p.id WHERE ( ppo.user_id = '%s' ) AND ( ppo.product_id = '%s' ) LIMIT 1;" % (row['user_id'],row['product_id'])
            info=self.odb.query(sql)
            res=info[0]
            print("%s   " % row['id'])
            sale=0
            if res['price']>0:
                sale=res['price']
            elif res ['add'] > 0:
                sale = res ['jiayi'] + res ['add']
            elif res ['percent'] > 0:
			    sale =res ['jiayi']* (1 +(res ['percent'] * 0.01))
            sale=round(sale,0)
            #print(sale)
            instesql="UPDATE `product_des_o2o` SET `jiayi`=%s,`sale`='%s',`status`=%s,`ygstatus`=%s,`cate_id`=%s WHERE ( `product_id` = %s ) AND ( `user_id` = %s )" % (res ['jiayi'],sale,row['status'],res['ygstatus'],res['cate_id'],row['product_id'],row['user_id'])
            #print(instesql)
            self.odb.save(instesql)
        self.odb.dbClose()

    def update_pid(self):
        self.odb.dbLoad()
        cats = self.odb.query(u"SELECT * from oc_category;")
        for row in cats:
            sql="SELECT * from oc_category_path WHERE category_id != path_id AND category_id = %s order by `level` DESC;" % (row['category_id'])
            info=self.odb.query(sql)
            pid=0
            if info:
                pid=info[0]['path_id']
            else:
                pid=0
            update_sql="UPDATE oc_category SET `parent_id`=%s where category_id=%s;" % (pid,row['category_id'])
            print update_sql

            self.odb.save(update_sql)
        self.odb.dbClose()

if __name__ == '__main__':
    desc=O2oDesc()
    desc.update_pid()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
